{{if fieldsinfo}}
<script>
var categories_hash={{JSON:categories_hash}};

// TODO add onchange to category_id
frm.ajajform("setWidgetOption","category_id","change",category_change_af);
category_change({{JSON:fields.category_id}});

function showhide_by_mask(re,arr,flag)
{
	for (var i=0;i<arr.length;i++) {
		if (!arr[i].id) continue;
		if (!(arr[i].id.match(re))) continue;
		if (flag) $(arr[i]).show(); else $(arr[i]).hide();
	}
}

function category_change_af(e)
{
	var cat_id=e.data.tag.val();
	category_change(cat_id);
	mif.ajajform_market_item_fields("option","category_id",cat_id);
}

function category_change(cat_id)
{
	var c=categories_hash[cat_id];
	if (!c) return console.log("Category id="+cat_id+" not found - strange");
	showhide_by_mask(/frm-\d+__abstract/,$(".afw-textareaedit"),c.has_abstract);
	showhide_by_mask(/frm-\d+__body2/,$(".afw-textareaedit"),c.has_body2);
}

var mif=frm.ajajform("insertWidget",null,null,{
	type:"market_item_fields",
	name:"mif",
	category_id:{{JSON:item.category_id}},
	l10ns: {{JSON:l10n_all}},
	dictionaries: {{JSON:fieldsinfo}},
	values: {{JSON:mif_values}}
});
</script>
{{/if}}
